package com.kfm.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;

@WebServlet("/getCookie")
public class GetCookieServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        // 获取 Cookie
        Cookie[] cookies = req.getCookies();
        for (Cookie cookie : cookies) {
            // getName 键 getValue 值
            String name = cookie.getName();
            String value = cookie.getValue();
            System.out.println(name + " : " + URLDecoder.decode(value, StandardCharsets.UTF_8));
        }
    }
}
